package cn.com.dycc.sys_manage.rolesysmanagement.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.dycc.common.log.Constants;
import cn.com.dycc.common.log.SystemServiceLog;
import cn.com.dycc.sys_manage.rolesysmanagement.dao.RoleDao;
import cn.com.dycc.sys_manage.rolesysmanagement.dao.RoleMenuDao;
import cn.com.dycc.sys_manage.rolesysmanagement.dao.RoleUserDao;
import cn.com.dycc.sys_manage.rolesysmanagement.entity.Role;
import cn.com.dycc.sys_manage.rolesysmanagement.entity.RoleInfo;
import cn.com.dycc.sys_manage.rolesysmanagement.entity.RoleResult;
import cn.com.dycc.util.IntegerUtil;
import cn.com.dycc.util.Paging;


/**
 * 
 * Description: 角色信息业务处理层
 * Author: linfangan
 * Version: 1.0
 * Create Date Time: 2017年8月4日.
 * Update Date Time: 2017年8月8日 下午4:34:05
 * @see
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService{
	
	@Resource(name="roleDao")
	private RoleDao roleDao;
	
	@Resource(name="roleUserDao")
	private RoleUserDao roleUserDao;
	
	@Resource(name="roleMenuDao")
	private RoleMenuDao roleMenuDao;
	
	@Resource(name="paging")
	private Paging paging;

	//查找全部角色信息
	@Transactional
	public List<RoleResult> findAll(String org_id,Integer page,Integer limit) throws RoleNotFoundException {
		
		Integer start = null;
		
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}
		
		List<RoleResult> list = new ArrayList<RoleResult>();
		try {
			list = roleDao.findAll(org_id, start, limit);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RoleNotFoundException("查询失败！请稍后重试");
		}

		return list;
	}
	
	//删除角色信息
	@SystemServiceLog(description=Constants.T_ROLE_DELETE)
	public String deleteRoleByRoleNo(String role_no,String org_id) throws RoleNotFoundException {
		if(role_no==null){
			throw new RoleNotFoundException("请选择删除的角色！");
		}
		
		if("0".equals(role_no)||"9".equals(role_no)){
			throw new RoleNotFoundException("该角色不能删除！");
		}
		
		RoleResult role = roleDao.findRoleByRoleNo(role_no,org_id);
		if(role==null){
			throw new RoleNotFoundException("该角色不存在");
		}
		
		List<String> list_user = roleUserDao.findUserIdByRoleNo(role_no);
		if(list_user.size()>0){
			throw new RoleNotFoundException("该角色还有用户在使用！");
		}
		
//		List<Map<String, Object>> list_menu = roleMenuDao.findMenNoByRoleNo(role_no);
//		if(list_menu.size()>0){
//			throw new RoleNotFoundException("该角色已经分配了菜单！");
//		}
		
		int del = roleDao.deleteRoleByRoleNo(role_no,org_id);
		
		if(del<=0){
			throw new RoleNotFoundException("删除失败！");
		}
		roleUserDao.deleteRoleUserByRoleNo(role_no);
		return "删除成功！";
	}
	
	//判断角色是否存在
	@Transactional
	public boolean addRole(Role role) throws RoleNotFoundException {
		String opt_user_id = role.getOpt_user_id();
		if(opt_user_id==null){
			throw new RoleNotFoundException("请登录！");
		}
		String org_id = role.getOrg_id();
		
		String role_no = role.getRole_no();
		String role_name = role.getRole_name();
		if(role_no==null||role_no.trim().isEmpty()){
			throw new RoleNotFoundException("角色编号不能为空");
		}
		//判断为整型
		boolean b = IntegerUtil.isNumeric(role_no);
		if(b==false){
			throw new RoleNotFoundException("角色编号请填写为整型!");
		}
		if(role_name==null||role_name.trim().isEmpty()){
			throw new RoleNotFoundException("角色名称不能为空");
		}
		RoleResult sel_roleno = roleDao.findRoleByRoleNo(role_no,org_id);
		if(sel_roleno!=null){
			throw new RoleNotFoundException("角色编号已被使用");
		}
		Role sel_rolename = roleDao.findRoleByRoleName(role_name,org_id);
		if(sel_rolename!=null){
			throw new RoleNotFoundException("角色名称已被使用");
		}
		return true;
	}
	
	//查询所有的角色信息，用于用户的角色分配，不含隐藏
	public List<RoleInfo> findAllNameOf(String org_id) throws RoleNotFoundException {
		
		List<RoleInfo> listRole = new ArrayList<RoleInfo>();
		try {
			listRole = roleDao.findAllNameOf(org_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listRole;
	}
	
	//角色列表统计
	public String findRoleListCount(HttpServletRequest request) throws RoleNotFoundException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		return roleDao.findRoleListCount(org_id);
	}

}
